package com.wangfei.thread.volatiletest;

import java.util.concurrent.TimeUnit;

public class T {
    static class Data{
        //       volatile   int number =0;
        int number =0;
        public void add()
        {
            this.number = number +1;
        }
    }
    // two threads, one work, one main
    private static void testVolatile() {
        Data myData = new Data();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"\t come in");
            try {
                TimeUnit.SECONDS.sleep(2);
                myData.add();
                System.out.println(Thread.currentThread().getName()+"\t update number value :"+myData.number);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "workThread").start();

        // main thread, second
        while (myData.number == 0){
            //main thread looking for 0
        }
        System.out.println(Thread.currentThread().getName()+"\t mission is over");
        System.out.println(Thread.currentThread().getName()+"\t mission is over, main get number is:"+myData.number);
    }
    public static void main(String[] args) {
        testVolatile();
    }
}
